package com.mapfinal.server.service.vectortile;

import java.io.IOException;

import javax.servlet.ServletOutputStream;

import com.jfinal.log.Log;
import com.jfinal.render.Render;

public class VectorTileRender extends Render {
	private static Log log = Log.getLog(VectorTileRender.class);
	
	public static final String MVT = "application/x-protobuf;type=mapbox-vector";
	public static final String GEOJSON = "application/json;type=geojson";
	public static final String TOPOJSON = "application/json;type=topojson";
	
    private byte[] tile;
    private String contentType;

    public VectorTileRender(byte[] tile, String contentType) {
        this.tile=tile;
        this.contentType=contentType;
    }

    public void render() {
        ServletOutputStream sos = null;
        try {
            //设置头信息,内容处理的方式,attachment以附件的形式打开,就是进行下载,并设置下载文件的命名
//            response.setHeader("Content-Disposition","attachment;filename="+file.getName());
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType(contentType);
            sos=response.getOutputStream();
            sos.write(tile);
        } catch (Exception e) {
            log.error("矢量切片render出错:"+e.getLocalizedMessage(),e);
            throw new RuntimeException(e);
        } finally {
            if (sos != null)
                try {
                    sos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}
